package com.iflytek.education.controller;

import com.iflytek.common.annotation.Log;
import com.iflytek.common.config.RuoYiConfig;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.core.page.TableDataInfo;
import com.iflytek.common.enums.BusinessType;
import com.iflytek.common.utils.file.FileUploadUtils;
import com.iflytek.common.utils.file.FileUtils;
import com.iflytek.common.utils.poi.ExcelUtil;
import com.iflytek.education.domain.EduVideo;
import com.iflytek.education.service.IEduVideoService;
import com.iflytek.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 课程视频Controller
 *
 * @author isahino
 * @date 2023-09-16
 */
@RestController
@RequestMapping("/edu/video")
public class EduVideoController extends BaseController {
    @Autowired
    private IEduVideoService eduVideoService;

    @Autowired
    private ServerConfig serverConfig;
    /**
     * 查询课程视频列表
     */
    @PreAuthorize("@ss.hasPermi('edu:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduVideo eduVideo) {
        startPage();
        List<EduVideo> list = eduVideoService.selectEduVideoList(eduVideo);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('edu:video:upload')")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 导出课程视频列表
     */
    @PreAuthorize("@ss.hasPermi('edu:video:export')")
    @Log(title = "课程视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduVideo eduVideo) {
        List<EduVideo> list = eduVideoService.selectEduVideoList(eduVideo);
        ExcelUtil<EduVideo> util = new ExcelUtil<>(EduVideo.class);
        util.exportExcel(response, list, "课程视频数据");
    }

    /**
     * 获取课程视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('edu:video:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(eduVideoService.selectEduVideoById(id));
    }

    /**
     * 新增课程视频
     */
    @PreAuthorize("@ss.hasPermi('edu:video:add')")
    @Log(title = "课程视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduVideo eduVideo) {
        return toAjax(eduVideoService.insertEduVideo(eduVideo));
    }

    /**
     * 修改课程视频
     */
    @PreAuthorize("@ss.hasPermi('edu:video:edit')")
    @Log(title = "课程视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduVideo eduVideo) {
        return toAjax(eduVideoService.updateEduVideo(eduVideo));
    }

    /**
     * 删除课程视频
     */
    @PreAuthorize("@ss.hasPermi('edu:video:remove')")
    @Log(title = "课程视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(eduVideoService.deleteEduVideoByIds(ids));
    }


}
